/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.badlogicgames.superjumper;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;

public class Pigeon extends DynamicGameObject {
	public static final int BOB_STATE_NORMAL = 0;
	public static final int BOB_STATE_FALL = 1;
	public static final int BOB_STATE_DIED = 2;
	public static final int BOB_STATE_HIT = 3;

	public boolean lastHitted = false;
	public static final float BOB_JUMP_VELOCITY = 11;
	public static final float BOB_WIDTH = 0.8f;
	public static final float BOB_HEIGHT = 0.8f;

	public static float BOB_MOVE_VELOCITY = 6f;
	public static float BOB_GROUND_SPEED = 3f;

	public static float BOB_hit_VELOCITY = 1f;
	private float touchIncrement = 0f;

	int state;
	float stateTime;
	float lastStateTime;
	public static float health; // health of pigeon from 0 to 100
	public static float MAX_HEALTH = 100;
	public static int shield; // shield of pigeon from 0 to 3
	public static float MAX_SHEILD = 100;
	public static float stamina; // stamina of pigeon from 0 to 100 ,, when it
									// is 0
	// pigeon can not go fast
	public static float MAX_STAMINA = 2000;

	public Pigeon(float x, float y) {
		super(x, y, BOB_WIDTH, BOB_HEIGHT);
		state = BOB_STATE_NORMAL;
		health = 100;
		stateTime = 0;
		stamina = MAX_STAMINA;
	}

	public void update(float deltaTime, float accelX, float accelY,
			boolean isTouched) {

		// if the user is touching the screen, the pigeon fly upward while
		// losing stamina.
		// if stamina reaches 0, the pigeon cannot fly up
		// if not touching the screen ie not flying upward, the pigeon slowly
		// starts to gain stamina.

		if (isTouched && stamina > 0) {
			touchIncrement = touchIncrement + 0.2f; // touchIncrement is the
													// delta distance that the
													// pigeon moves in upward
													// direction
			stamina = stamina - 2;
		} else {
			if (touchIncrement > -4) {
				touchIncrement = touchIncrement - 0.2f;
			}

			if (stamina < MAX_STAMINA) {
				stamina = stamina + 1;
			}
		}
		if (position.y >= Settings.WORLD_HEIGHT) {
			touchIncrement = 0;
		} // bob.position.y is the place that we dont want to go up of that

		if (state != Pigeon.BOB_STATE_DIED) {
			velocity.y = (float) touchIncrement;
			if (accelY <= -5) {
				accelY = -5;
			}
			if (accelY >= 5) {
				accelY = 5;
			}
			if (position.y <= Settings.GROUND_POSITION) {
				position.y = Settings.GROUND_POSITION; // don't let pigeon go
														// below the ground.
				velocity.x = accelY / 10 * Pigeon.BOB_GROUND_SPEED; // since
																	// pigeon is
																	// on
																	// ground,
																	// change
																	// his speed
																	// to ground
																	// speed.
			}
			velocity.x = accelY / 10 * Pigeon.BOB_MOVE_VELOCITY
					* BOB_hit_VELOCITY;
			Gdx.app.log("velocityX-accelY", velocity.x + " " + accelY);
		}
		BOB_hit_VELOCITY = 1;

		velocity.add(World.gravity.x * deltaTime, -World.gravity.y * deltaTime);

		position.add(velocity.x * deltaTime, velocity.y * deltaTime);
		bounds.x = position.x - bounds.width / 2;
		bounds.y = position.y - bounds.height / 2;
		Gdx.app.log("deltaTime", deltaTime + " ");

		if (position.y <= 0.5f)
			position.y = 0.5f;
		if (position.y >= World.WORLD_WIDTH - 1)
			position.y = World.WORLD_WIDTH - 1;

		stateTime += deltaTime;
		this.lastStateTime += deltaTime;

		if (this.lastStateTime > 5) { // no damage for some seconds
			this.lastStateTime = 0;
			this.lastHitted = false;
			state = BOB_STATE_NORMAL;
		}
	}

	public void hitSquirrel(Vector2 velocity) {

		if (!this.lastHitted) {
			this.lastHitted = true;
			health = health - 10;
			state = BOB_STATE_HIT;
		}
		if (health <= 0) {
			velocity.set(0, 0);
			state = BOB_STATE_DIED;

		}
		stateTime = 0;
		BOB_hit_VELOCITY = -1;
	}

	public void hitPlatform() {
		// velocity.y = BOB_JUMP_VELOCITY;
		BOB_hit_VELOCITY = -1;
//		health = health - 3;
		// state = BOB_STATE_NORMAL;
		stateTime = 0;
	}

	public void hitSpring() {
		velocity.y = BOB_JUMP_VELOCITY * 1.5f;
		// state = BOB_STATE_NORMAL;
		stateTime = 0;
	}
}
